package file.listener;

import file.FileFrame;
import file.constant.MenuItemEnum;
import file.handle.FileHandle;
import file.handle.FilePannel;
import file.handle.impl.FileHandleImpl;
import file.handle.impl.FilePannelImpl;
import file.model.FileModel;
import file.untils.StringUntils;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

/**
 * 菜单子项Listen
 * @Author: zou
 * @email: 704572528@qq.com
 * @Date: 2020/10/30 13:22
 */
public class MenuItemListener extends FileFrameAbstract implements MouseListener {

    private FileHandle fileHandle = new FileHandleImpl();
    private MenuItemEnum menuItemEnum;
    private FilePannel filePannel = new FilePannelImpl(fileFrame);

    public MenuItemListener(FileFrame fileFrame, MenuItemEnum menuItemEnum) {
        super(fileFrame);
        this.menuItemEnum = menuItemEnum;
    }

    /**
     * 菜单点击 干枚举所要做的事情
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {

      /*  String pathStr = getPathLable();
        FileModel fm = new FileModel();
        fm.setPath(getPathLable());
        switch (menuItemEnum) {
            case createFile:
                fileHandle.createFile(fm);
                break;
            case createFolder:
                fileHandle.createFolder(fm);
                break;
            case copy:
                fileHandle.inputFile(fileFrame.getHiddleLable().getText());
                break;
            case paste:
                fileHandle.copyFile(null, getPathLable());
                break;
            case remove:
                fileHandle.removeFile(fileFrame.getHiddleLable().getText());
                break;
            *//*case reName:
                fileHandle.changeFileName()
                break;*//*

        }*/


    }

    @Override
    public void mousePressed(MouseEvent e) {
        String pathStr = getPathLable();
        FileModel fm = new FileModel();
        fm.setPath(pathStr);
        switch (menuItemEnum) {
            case createFile:
                fileHandle.createFile(fm);
                updateUi();
                break;
            case createFolder:
                fileHandle.createFolder(fm);
                updateUi();
                break;
            case copy:
                fileHandle.inputFile(fileFrame.getHiddleLable().getText());
                break;
            case paste:
                fileHandle.copyFile(null, getPathLable());
                updateUi();
                break;
            case remove:
                fileHandle.removeFile(fileFrame.getHiddleLable().getText());
                updateUi();
                break;
            case reName:
                this.reName();
                break;
            case back:
                this.fileBack();
                break;
            case shear:
                fileHandle.shearFile(fileFrame.getHiddleLable().getText());
                break;

        }
    }

    /**
     * 修改名称
     */
    private void reName() {
        JDialog dl = fileFrame.getDialog1();
        dl.setLocationRelativeTo(fileFrame);
        JTextArea cl = fileFrame.getChangeLable();
        cl.setText("");
        if (!dl.isShowing()) {
            String path = fileFrame.getHiddleLable().getText();
            String lastName = StringUntils.getLastName(path);
            cl.setText(lastName);
            dl.show();
        }
    }

    private void updateUi() {
        File file = new File(getPathLable());
        JPanel filePanel = this.fileFrame.getFilePanel();
        // 清除布局
        filePannel.removeFile(filePanel);
        // 文件处理
        filePannel.fileHandel(file,filePanel);
        // 更新Ui
        filePannel.updateUI(filePanel);
    }

    /**
     * 返回上一层
     */
    protected  void fileBack() {

        String pathLable = this.getPathLable();
        if (!pathLable.isEmpty()) {
            int iIndex = pathLable.lastIndexOf("\\");
            if (iIndex != -1) {
                pathLable = pathLable.substring(0,iIndex) + "\\";
            } else {
                pathLable = pathLable + "\\";
            }
            this.setPathLablePath(pathLable);

            cleanPannel(pathLable);
        }

    }

    /**
     * 清扫
     * @param pathLable
     */
    private void cleanPannel(String pathLable) {
        filePannel.removeFile(fileFrame.getFilePanel());
        filePannel.fileHandel(new File(pathLable),fileFrame.getFilePanel());
        fileFrame.getFilePanel().updateUI();
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
